package pw.hais.http;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;

import org.json.JSONObject;

import java.io.File;
import java.util.Map;

import pw.hais.app.UtilConfig;
import pw.hais.http.base.BaseHttp;
import pw.hais.http.base.Method;
import pw.hais.http.base.OnHttpListener;
import pw.hais.http.image.CacheManager;
import pw.hais.utils.R;

/**
 * 基于 OkHttp 的网络请求封装
 * Created by Hais1992 on 2015/8/25.
 */
public class Http {

    public static void get(String url, Map<String, String> params, OnHttpListener<?> listener) {
        get(url, null, params, listener);
    }

    public static void get(String url, Map<String, String> header, Map<String, String> params, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.GET, url, header, params, null, listener);
    }

    public static void post(String url, Map<String, String> params, OnHttpListener<?> listener) {
        post(url, null, params, listener);
    }

    public static void post(String url, Map<String, String> header, Map<String, String> params, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.POST, url, header, params, null, listener);
    }

    public static void post(String url, Map<String, String> header, Map<String, String> params, byte[] body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.POST, url, header, params, body, listener);
    }

    public static void post(String url, Map<String, String> header, Map<String, String> params, Map<String, String> body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.POST, url, header, params, body, listener);
    }

    public static void post(String url, Map<String, String> header, Map<String, String> params, String body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.POST, url, header, params, body, listener);
    }

    public static void post(String url, Map<String, String> header, Map<String, String> params, JSONObject body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.POST, url, header, params, body, listener);
    }

    public static void post(String url, Map<String, String> header, Map<String, String> params, File body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.POST, url, header, params, body, listener);
    }


    public static void put(String url, Map<String, String> header, Map<String, String> params, JSONObject body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.PUT, url, header, params, body, listener);
    }

    public static void delete(String url, Map<String, String> header, Map<String, String> params, String body, OnHttpListener<?> listener) {
        BaseHttp.addHttpRequest(Method.DELETE, url, header, params, body, listener);
    }


    /*-----------------------------HTTP文件上传-------------------------------------*/
    public static void updateFile(String url, Map<String, String> header, Map<String, String> params, File[] files, String[] fileKeys, OnHttpListener<?> listener) {
        BaseHttp.addUpdateRequest(url, header, params, files, fileKeys, listener);
    }

    public static void updateFile(String url, Map<String, String> header, Map<String, String> params, File file, String fileKey, OnHttpListener<?> listener) {
        BaseHttp.addUpdateRequest(url, header, params, new File[]{file}, new String[]{fileKey}, listener);
    }

    /*-----------------------------图片下载显示-------------------------------------*/
    public static void displayImage(ImageView imageView, int resId) {
        Glide.with(imageView.getContext()).load(resId).placeholder(R.drawable.image_default).error(R.drawable.image_error).crossFade().into(imageView);
    }

    public static void displayImage(ImageView imageView, File file) {
        Glide.with(imageView.getContext()).load(file).placeholder(R.drawable.image_default).error(R.drawable.image_error).crossFade().into(imageView);
    }

    public static void displayImage(ImageView imageView, String url) {
        Glide.with(imageView.getContext()).load(url).placeholder(R.drawable.image_default).error(R.drawable.image_error).crossFade().into(imageView);
    }

    public static void cacheImage(Context context,String url) {
        cacheImage(context, url, null);
    }

    public static void cacheImage(Context context,String url, final OnHttpListener<Bitmap> listener) {
        Glide.with(context).load(url).asBitmap().placeholder(R.drawable.image_default).error(R.drawable.image_error).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                if (listener != null) listener.onSuccess(null, resource);
            }
        });
    }

    public static Bitmap getCacheBitmap(Context context,String url) {
       try {
           return Glide.with(context).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).into(1024,1024).get();
       }catch (Exception e){
           e.printStackTrace();
       }
        return null;
    }

    /*-----------------------------文件　　下载-------------------------------------*/
    public static void download(String url, String fileDir, OnHttpListener<String> listener) {
        BaseHttp.addDownloadRequest(url, fileDir, true, listener);
    }

    public static void download(String url, String fileDir, boolean isCover, OnHttpListener<String> listener) {
        BaseHttp.addDownloadRequest(url, fileDir, isCover, listener);
    }


    /*-----------------------------取消一个请求-------------------------------------*/
    public static void cancel(String url) {
        BaseHttp.cancel(url);
    }


}
